/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mapeServer;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marco
 */
public class UDPService implements Connection{
    
    private int port;
    private Service servant;
    private DatagramSocket serverSocket;
    private byte[] buffer;
    
    public UDPService(Service servant, int port){
        this.servant = servant;
        this.port = port;
    }
    
    @Override
    public void run() {
        try {
            serverSocket = new DatagramSocket(port);
            
            while(true){
            buffer = new byte[1024];
            
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
            
            serverSocket.receive(packet);
            
            String message = new String(packet.getData());
            
            servant.enqueue(message);
            }
            
            
        } catch (IOException ex) {
            Logger.getLogger(UDPService.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    
}
